import sys
from PySide2.QtWidgets import QApplication, QMainWindow
from PySide2.QtCore import Slot,QThreadPool
#from PySide2.QtGui import QDoubleValidator

from MainWindow import Ui_MainWindow
from myThread import downloadNovel
class Gui(QMainWindow):
    def __init__(self):
        super(Gui, self).__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.setWindowTitle("小说下载器")
        self.ui.txtUrl.setPlaceholderText("请输入小说链接")
        self.ui.txtPath.setPlaceholderText("请输入保存位置")
        self.ui.txtName.setPlaceholderText("请输入小说名称")
                
        self.savePath=self.ui.txtPath.text()
        self.novelName=self.ui.txtName.text()        
        self.Url = self.ui.txtUrl.text()
        
        self.threadpool = QThreadPool() 


        
        self.ui.txtUrl.textChanged.connect(self.Url_changed)
        self.ui.txtPath.textChanged.connect(self.Path_changed)
        self.ui.txtName.textChanged.connect(self.Name_changed)
        
        self.ui.btnDownload.clicked.connect(self.download)
        
    @Slot()
    def StopThread(self): 
        self.Thread.Stop() 

    @Slot()
    def PauseThread(self): 
        
        if self.ui.btnPause.text() =="暂停下载":
            self.Thread.Pause("暂停")
            self.ui.btnPause.setText("继续下载")
        else:
            self.Thread.Pause("继续")
            self.ui.btnPause.setText("暂停下载")
    @Slot()
    def download(self):  
        self.ui.txtOutput.setText("")
        #print(self.url)
        #print(self.novelList)
        self.ui.txtUrl.setEnabled(False)
        self.ui.txtName.setEnabled(False)
        self.ui.txtPath.setEnabled(False)
        self.ui.btnDownload.setEnabled(False)
        self.Thread = downloadNovel(self.Url,self.novelName,self.savePath)
        
        self.Thread.Signals.progress.connect(self.printMessage)
        self.Thread.Signals.over.connect(self.printMessage) 
        self.Thread.Signals.pause.connect(self.printMessage) 
        self.Thread.Signals.stop.connect(self.printMessage)
        self.Thread.Signals.error.connect(self.printMessage)
        self.ui.btnStop.clicked.connect(self.StopThread)
        self.ui.btnPause.clicked.connect(self.PauseThread)
        
        self.threadpool.start(self.Thread)
      
    @Slot()
    def printMessage(self,message):        
        self.ui.txtOutput.append(message)
        if ("停止" or "结束") in message:
            
            self.ui.txtUrl.setEnabled(True)
            self.ui.txtName.setEnabled(True)
            self.ui.txtPath.setEnabled(True)
            self.ui.btnDownload.setEnabled(True)
            self.ui.btnPause.setEnabled(False)
            self.ui.btnStop.setEnabled(False)
        elif "暂停" in message:
            #print("GAGAG")
            self.ui.btnPause.setEnabled(False)
            self.ui.btnStop.setEnabled(False)
        elif "错误" in message:
            self.ui.txtUrl.setEnabled(True)
            self.ui.txtName.setEnabled(True)
            self.ui.txtPath.setEnabled(True)
            self.ui.btnDownload.setEnabled(True)
            self.ui.btnPause.setEnabled(True)
            self.ui.btnStop.setEnabled(True)
        else: 
            #print(message)
            self.ui.btnPause.setEnabled(True)
            self.ui.btnStop.setEnabled(True)
    @Slot()
    def Url_changed(self):
        self.Url=self.ui.txtUrl.text()
        
    @Slot()
    def Path_changed(self):
        self.savePath=self.ui.txtPath.text()
        
    @Slot()
    def Name_changed(self):
        self.novelName=self.ui.txtName.text() 

        
if __name__ == "__main__":
    app=QApplication([])

    window = Gui()
    window.show()

    sys.exit(app.exec_())